package org.jsbd.boss.task;

import com.alibaba.fastjson.JSONObject;
import edu.hziee.common.queue.DelayExecuteBuffer;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.bto.HttpResult;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.domian.IpRange;
import org.jsbd.boss.domian.channel.UserClientInfo;
import org.jsbd.boss.enums.HistoryUserTableEnum;
import org.jsbd.boss.service.ClientManager;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc
 * @Date 2018/10/18 16:20
 * @Author xupengcheng [xupengcheng@zhexinit.com]
 */
public class OldUserClientTask implements Runnable, Serializable {

    public static final Logger logger = LoggerFactory.getLogger(FileUploadTask.class);
    private static final long serialVersionUID = 1L;

    private RedisClient redisClient;

    private ClientManager clientManager;

    private DelayExecuteBuffer<UserClientInfo> userClientBatchInsertBuffer;

    private DelayExecuteBuffer<UserClientInfo> userClientBatchUpdateBuffer;

    private HttpResult httpResult;
    private  IpRange ipRange;


    public OldUserClientTask(HttpResult httpResult, IpRange ipRange){
        this.httpResult = httpResult;
        this.ipRange = ipRange;
    }

    @Override
    public void run() {

        redisClient = SpringContextUtils.getBean("redisClient");
        clientManager = SpringContextUtils.getBean(ClientManager.class);
        Long time = (Long) redisClient.getHash(CacheKey.ONLINE_IMSI_RECORD, httpResult.getImsi());
        long now = System.currentTimeMillis();
        if (time == null) {
            UserClientInfo userClient = clientManager.initUserClientinfo(httpResult, ipRange);
            userClient.setTableName(HistoryUserTableEnum.HISTORY.getCode());
            addExtParamInfoToOss(httpResult, userClient);
            redisClient.putHash(CacheKey.ONLINE_IMSI_RECORD, httpResult.getImsi(), now);
            userClientBatchInsertBuffer = SpringContextUtils.getBean("userClientBatchInsertBuffer");
            userClientBatchInsertBuffer.add(userClient);
        } else if (now - time > 86400 * 1000 * 3) {
            // 3天以上就更新一下
            UserClientInfo userClient = clientManager.initUserClientinfo(httpResult, ipRange);
            userClient.setTableName(HistoryUserTableEnum.HISTORY.getCode());
            addExtParamInfoToOss(httpResult, userClient);
            userClientBatchUpdateBuffer = SpringContextUtils.getBean("userClientBatchUpdateBuffer");
            userClientBatchUpdateBuffer.add(userClient);
            redisClient.putHash(CacheKey.ONLINE_IMSI_RECORD, httpResult.getImsi(), now);
        }
    }

    /**
     * 添加额外的信息到oss addExtParamInfoToOss:TODO 请描述该方法是做什么用途的
     *
     * @param httpResult
     * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
     */
    private void addExtParamInfoToOss(HttpResult httpResult, UserClientInfo userClient) {
        String imei = userClient.getImei();
        if (StringUtils.isBlank(imei)) {
            logger.error("addExtParamInfoToOss imsi[{}], imei is null", httpResult.getImsi());
            return;
        }

        if (StringUtils.isBlank(httpResult.getBht_mac()) && StringUtils.isBlank(httpResult.getCpuProcessor()) && StringUtils.isBlank(httpResult.getCpuHardware())
                && StringUtils.isBlank(httpResult.getCpuRevision()) && StringUtils.isBlank(httpResult.getLauncherPackageName()) && StringUtils.isBlank(httpResult.getArpInfo())
                && StringUtils.isBlank(httpResult.getSpaceInfo()) && StringUtils.isBlank(httpResult.getDfOutput())) {
            return;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bht_mac", httpResult.getBht_mac());
        jsonObject.put("cpuProcessor", httpResult.getCpuProcessor());
        jsonObject.put("cpuHardware", httpResult.getCpuHardware());
        jsonObject.put("cpuRevision", httpResult.getCpuRevision());
        jsonObject.put("launcherPackageName", httpResult.getLauncherPackageName());
        jsonObject.put("arpInfo", httpResult.getArpInfo());
        jsonObject.put("spaceInfo", httpResult.getSpaceInfo());
        jsonObject.put("dfOutput", httpResult.getDfOutput());
        jsonObject.put("cpuSerial", httpResult.getCpuSerial());
        jsonObject.put("cpuName", httpResult.getCpu());

        Map<String, String> dataMap = new HashMap<String, String>();
        dataMap.put("imei", imei);
        dataMap.put("data", jsonObject.toJSONString());
        ExtraParamOssUploadTask.addTask(dataMap);
    }
}
